<?php

/**
* 自动执行
*
* @package		Hooloo framework
* @author 		Bill
* @copyright 	Hooloo Co.,Ltd
* @version		1.0
* @release		2017.05.08
*/
defined('BASEPATH') OR exit('No direct script access allowed');

class Auto extends Controller{
	public function __construct(){
		parent::__construct();
	}
	
	/**
     * 自动运行
     */
	public function run() {
		/*
			crontab命令：
			* 9-12 * * * /data/auto/crontab.sh
			* 13-15 * * * /data/auto/crontab.sh
			
			* 9-12 * * * wget -T 1 http://stock.hnhuiyu.com/auto/run
			* 9-12 * * * sleep 10; wget -T 1 http://stock.hnhuiyu.com/auto/run
			* 9-12 * * * sleep 20; wget -T 1 http://stock.hnhuiyu.com/auto/run
			* 9-12 * * * sleep 30; wget -T 1 http://stock.hnhuiyu.com/auto/run
			* 9-12 * * * sleep 40; wget -T 1 http://stock.hnhuiyu.com/auto/run
			* 9-12 * * * sleep 50; wget -T 1 http://stock.hnhuiyu.com/auto/run
			* 13-15 * * * wget -T 1 http://stock.hnhuiyu.com/auto/run
			* 13-15 * * * sleep 10; wget -T 1 http://stock.hnhuiyu.com/auto/run
			* 13-15 * * * sleep 20; wget -T 1 http://stock.hnhuiyu.com/auto/run
			* 13-15 * * * sleep 30; wget -T 1 http://stock.hnhuiyu.com/auto/run
			* 13-15 * * * sleep 40; wget -T 1 http://stock.hnhuiyu.com/auto/run
			* 13-15 * * * sleep 50; wget -T 1 http://stock.hnhuiyu.com/auto/run
		*/
		//开市状态
		$holiday = get_holiday();
		
		//更新股票(9点-9点30)
		if (time() > strtotime(date("Y-m-d 9:00")) && time() < strtotime(date("Y-m-d 9:30"))) {
			get_new_stock();
		}

		//正常开市时间执行（一般情况周一~周五，节假日特殊处理！）
		if ($holiday == true && $this->config_sys["system_switch"] != 0) {
			//自动委托
			auto_trust();

			//自动爆仓（分批次处理，每30秒检查一次，防止系统卡死。）
			$ss = date("s");
			if ($ss % 30 == 0) {
				auto_force();
			}

			//除权除息(14点57-15点00)
			if (time() > strtotime(date("Y-m-d 14:57"))) {
				auto_drxd();
			}
			
		}
	}
	
	/* public function test () {
		$sql = "select * from log_bill where id >= 26826 and uid = 232 order by id asc";
		$list = $this->db->query($sql)->result_array();
		$up_sql = "";
		foreach ($list as $key => $val) {
			if ($key > 0) {
				$sql = "select * from log_bill where id = " . $list[$key-1]["id"];
				$row = $this->db->query($sql)->row_array();
				
				$old_finance = $row["old_finance"] + $row["finance"];
				$old_amount = $row["old_amount"] + $row["amount"];
				$old_gain = $row["old_gain"] + $row["gain"];
				
				$up_sql .= $sql = "update log_bill set old_finance = $old_finance, old_amount = $old_amount, old_gain = $old_gain where id = " . $val["id"] . ";" . PHP_EOL;
				$this->db->query($sql);
			}
		}
		
		file_put_contents("./232.log", $up_sql);
	} */
}
